package com.asdf.ui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class SubscribeDialog extends Dialog
{

	private static final long	serialVersionUID	= - 2430550986118715391L;
	
	private static final String FIRST_NAME_LABEL_CAPTION		= "First name:";
	private static final String SECOND_NAME_LABEL_CAPTION		= "Second name:";
	private static final String ADDRESS_LABEL_CAPTION  	 		= "Address:";
	private static final String	EMAIL_LABEL_CAPTION		 		= "Email:";
	private static final String	USER_ID_LABEL_CAPTION     		= "ID number:";
	private static final String	AVIACOMPANY_LABEL_CAPTION		= "Aviation company:";
	private static final String	USER_NICK_NAME_LABEL_CAPTION	= "Login nick name:";

	private static final String	SUBSCRIBE	= "Subscribe";
	private static final String	CANCEL		= "Cancel";

	private JCheckBox	checkSendingOption = new JCheckBox( "Send printed bill to your address?");	
	
	private JPanel inputPanel    = new JPanel( new GridLayout( 0,2 ));
	private JPanel buttonsPanel  = new JPanel( new FlowLayout( FlowLayout.CENTER));
	private JPanel checkPanel	 = new JPanel( new FlowLayout( FlowLayout.LEFT));
	
	private JLabel 		lblFirstName = new JLabel( FIRST_NAME_LABEL_CAPTION);
	private JTextField 	txtFirstName = new JTextField( );
	
	private JLabel 		lblSecondName = new JLabel( SECOND_NAME_LABEL_CAPTION);
	private JTextField	txtSecondName = new JTextField( );
	
	private JLabel 		lblUserId	  = new JLabel( USER_ID_LABEL_CAPTION);
	private JTextField  txtUserId	  = new JTextField( );
	
	private JLabel 		lblAddress = new JLabel( ADDRESS_LABEL_CAPTION);
	private JTextField	txtAddress = new JTextField( );
	
	private JLabel  	lblEmail 	= new JLabel( EMAIL_LABEL_CAPTION);
	private JTextField  txtEmail 	= new JTextField( );
	
	private JLabel 		lblAviaCompany = new JLabel( AVIACOMPANY_LABEL_CAPTION);
	private JTextField 	txtAviaCompany = new JTextField( );
	
	private JLabel 		lblUserNickName = new JLabel( USER_NICK_NAME_LABEL_CAPTION);
	private JTextField	txtUserNickName = new JTextField( );

	private JButton btnSubscribe = new JButton( SUBSCRIBE);
	private JButton btnCancel 	 = new JButton( CANCEL);

	protected void drawLabelsAndInputs( )
	{

		lblFirstName.setLabelFor( txtFirstName);
		inputPanel.add( lblFirstName);
		inputPanel.add( txtFirstName);
		
		lblSecondName.setLabelFor( txtSecondName);
		inputPanel.add( lblSecondName);
		inputPanel.add( txtSecondName);
		
		lblUserId.setLabelFor( txtUserId);
		inputPanel.add( lblUserId);
		inputPanel.add( txtUserId);
		
		lblUserNickName.setLabelFor( txtUserNickName);
		inputPanel.add( lblUserNickName);
		inputPanel.add( txtUserNickName);
		
		lblAddress.setLabelFor( txtAddress);
		inputPanel.add( lblAddress);
		inputPanel.add( txtAddress);
		
		lblEmail.setLabelFor( txtEmail);
		inputPanel.add( lblEmail);
		inputPanel.add( txtEmail);
		
		lblAviaCompany.setLabelFor( txtAviaCompany);
		inputPanel.add( lblAviaCompany);
		inputPanel.add( txtAviaCompany);
		
		buttonsPanel.add( btnSubscribe);
		buttonsPanel.add( btnCancel);
		
		checkPanel.add( checkSendingOption);
		
		TitledBorder title = BorderFactory.createTitledBorder( BorderFactory.createLineBorder(Color.black),
				  "Enter your login details:", TitledBorder.LEFT, TitledBorder.TOP);

		inputPanel.setBorder( title);
		add( inputPanel);
		add( checkPanel);
		add( buttonsPanel);
		getRootPane( ).setDefaultButton( btnSubscribe);
	}

	public SubscribeDialog( )
	{
		super( "New user subscription") ;
		drawLabelsAndInputs( );
		pack( );
	}
	
	public void registerEventHandlerCancelCommand( String commandName, ActionListener listener)
	{
		btnCancel.setActionCommand( commandName);
		btnCancel.addActionListener( listener);
	}
	
	public void registerEventHandlerSubscribeCommand( String commandName, ActionListener listener)
	{
		btnSubscribe.setActionCommand( commandName);
		btnSubscribe.addActionListener( listener);
	}

	public String getFirstName( )
	{
		return txtFirstName.getText( );
	}
	
	public String getLastName( )
	{
		return txtSecondName.getText( );
	}
	
	public String getUserId( )
	{
		return txtUserId.getText( );
	}
	
	public String getAddress( )
	{
		return txtAddress.getText( );
	}
		
	public String getEmail( )
	{
		return txtEmail.getText( );
	}
	
	public boolean sendHardCopyBill( )
	{
		return checkSendingOption.isSelected( );
	}
	
	public String getUserNickName( )
	{
		return txtUserNickName.getText( );
	}
	
	
	public String getAviationCompanyName( )
	{
		return txtAviaCompany.getText( );
	}
	
	/**
	 * @param args
	 */
	public static void main( String [ ] args)
	{
		SubscribeDialog dialog = new SubscribeDialog( );
		dialog.setVisible( true);
	}

}
